package org.example.pojo;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class CustomMultipartFile implements MultipartFile {

    private final File file;
    private final FileInputStream fileInputStream;

    public CustomMultipartFile(File file) throws IOException {
        this.file = file;
        this.fileInputStream = new FileInputStream(file);
    }

    @Override
    public String getName() {
        return file.getName(); // 返回文件名
    }

    @Override
    public String getOriginalFilename() {
        return file.getName(); // 原始文件名
    }

    @Override
    public String getContentType() {
        // 这里可以返回你需要的 MIME 类型，如果有需要可以根据文件类型动态判断
        return "audio/wav"; // 示例 MIME 类型
    }

    @Override
    public boolean isEmpty() {
        return file.length() == 0;
    }

    @Override
    public long getSize() {
        return file.length(); // 文件的字节大小
    }

    @Override
    public byte[] getBytes() throws IOException {
        return fileInputStream.readAllBytes(); // 读取文件并返回字节数组
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return fileInputStream; // 获取文件输入流
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        // 将文件内容传输到目标文件
        try (FileInputStream inputStream = new FileInputStream(file);
             FileOutputStream outputStream = new FileOutputStream(dest)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        }
    }
}
